<?php

namespace App\Http\Controllers\Wx;

use App\Http\Controllers\WxController;
use App\Models\Shop\GoodsClassify;
use App\Models\Shop\Store;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class IndexController extends WxController
{
    public function getIndex(Request $request)
    {
        $storeID = 1;
        $typeID = 0;
        if(isset($_COOKIE['storeID']) && is_numeric($_COOKIE['storeID']))
        {
            $storeID = $_COOKIE['storeID'];
        }
        if($request->has('storeID'))
        {
            $storeID = $request->input('storeID');
        }
        $goods = DB::table('shop_store_goods')
            ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
            ->where('shop_store_goods.storeID', $storeID)
            ->where('shop_store_goods.display', 1);
        if($request->has('typeID'))
        {
            $typeID = $request->input('typeID');
            $goods = $goods->where('shop_goods.classifyID',$typeID);
        }
        $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
            'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract')
            ->orderBy('id', 'desc')
            ->take(10)
            ->get();

        //获取分类
        $classify = GoodsClassify::all()->keyBy('id');
        //获取大学
        $store = Store::orderBy('id', 'desc')->get()->keyBy('id');


        return view('wx.index')->with(['goods' => $goods, 'classify' => $classify, 'store' => $store, 'storeID' => $storeID, 'typeID' => $typeID, 'user' => $this->user]);
    }

    public function postIndex(Request $request)
    {
        if ($request->has('page'))
        {
            $page = $request->input('page');
            $storeID = $request->input('storeID');
            $typeID = $request->input('typeID');
            //获取cookie购物车
            $cartOld = [];
            if(isset($_COOKIE['cart']))
            {
                $cartCookie = json_decode($_COOKIE['cart']);
                if(count($cartCookie) > 0)
                {
                    foreach($cartCookie as $c)
                    {
                        $cartOld[$c[0]] = $c;
                    }
                }
            }

            //获取物品
            $goods = DB::table('shop_store_goods')
                ->join('shop_goods', 'shop_store_goods.goodsID', '=', 'shop_goods.id')
                ->where('shop_store_goods.storeID', $storeID)
                ->where('shop_store_goods.display', 1);
            if($typeID)
            {
                $goods = $goods->where('shop_goods.classifyID', $typeID);
            }
            $goods = $goods->select('shop_store_goods.id', 'shop_store_goods.storeID', 'shop_store_goods.amount', 'shop_store_goods.realAmount', 'shop_store_goods.number', 'shop_store_goods.display',
                'shop_goods.classifyID', 'shop_goods.title', 'shop_goods.thumb', 'shop_goods.abstract')
                ->skip($page*10)
                ->take(10)
                ->orderBy('id', 'desc')
                ->get();
            if (count($goods) > 0)
            {
                $html = '';

                foreach ($goods as $v)
                {
                    $html .= '<li id="goods-'.$v->id.'">';
                    $html .= '<a href="#" class="item-link item-content">';
                    $html .= '<div class="item-media"><img data-src="'.action('ImgController@getPicture', ['sourceName' => $v->thumb, 'size' => '80x80']).'" width="80" class="lazy lazy-fadein"></div>';
                    $html .= '<div class="item-inner">';
                    $html .= '<div class="item-title-row closeLink">';
                    $html .= '<div class="item-title">'.$v->title.'</div>';
                    $html .= '<div class="item-after">'.$v->realAmount.' 円</div>';
                    $html .= '</div>';
                    $html .= '<div class="item-text">'.$v->abstract.'</div>';
                    $html .= '</div></a>';
                    $html .= '<div class="form-group shop-add">';
                    $html .= '<div class="col-xs-6"></div><div class="col-xs-6"><p class="buttons-row">';
                    if(count($cartOld) > 0 && isset($cartOld[$v->id]))
                    {
                        $html .= '<a href="#" class="button down"><i class="fa fa-cart-arrow-down fa-2x"></i></a>';
                        $html .= '<span class="button" data-id="'.$v->id.'" data-amount="'.$v->realAmount.'">'.$cartOld[$v->id][2].'</span>';
                    }else{
                        $html .= '<a href="#" class="button down hidden"><i class="fa fa-cart-arrow-down fa-2x"></i></a>';
                        $html .= '<span class="button" data-id="'.$v->id.'" data-amount="'.$v->realAmount.'"></span>';
                    }
                    $html .= '<a href="#" class="button plus"><i class="fa fa-cart-plus fa-2x"></i></a>';
                    $html .= '</p></div></div>';
                    $html .= '</li>';
                }

                $result = [
                    'code'      =>      0,
                    'date'      =>      $html
                ];
            }else{
                $result = [
                    'code'      =>      1,
                    'date'      =>      ''
                ];
            }
            return json_encode($result);
        }
    }
}
